package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.entity.PmsProduct;
import com.mall.entity.SmsNewCommodity;
import com.mall.service.PmsProductService;
import com.mall.service.SmsNewCommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "SmsCouponCategoryController", description = "新品推荐管理")
@RestController
@RequestMapping("/newcommodities")
public class SmsNewCommodityController {
    @Resource
    private SmsNewCommodityService smsNewCommodityService;

    @Resource
    private PmsProductService pmsProductService;

    @ApiOperation(value = "新增新品")
    @PostMapping
    public Result save(@RequestBody SmsNewCommodity smsNewCommodity) {
        boolean flag = smsNewCommodityService.save(smsNewCommodity);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }

    @ApiOperation(value = "根据id删除新品")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsNewCommodityService.deleteById(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "根据id查找新品")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsNewCommodity smsNewCommodity = new SmsNewCommodity();
        smsNewCommodity = smsNewCommodityService.getById(id);
        Integer code = smsNewCommodity !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsNewCommodity !=null ? "successful":"failed,please try again";
        return new Result(smsNewCommodity,code,msg);
    }

    @ApiOperation(value = "查找全部新品")
    @GetMapping
    public Result getAll(){
        List<SmsNewCommodity> list = smsNewCommodityService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @ApiOperation(value = "批量增加新品推荐")
    @PostMapping("/addlist")
    public Result addList(@RequestBody List<Integer> ids){
        List<SmsNewCommodity> list = new ArrayList<SmsNewCommodity>();
        PmsProduct product = new PmsProduct();
        for(int i=0;i<ids.size();i++)
        {
            SmsNewCommodity newProduct = new SmsNewCommodity();
            product = pmsProductService.getById(ids.get(i));
            newProduct.setProductId(product.getId());
            newProduct.setProductName(product.getName());
            newProduct.setRecommendStatus(0);
            newProduct.setSort(0);
            list.add(newProduct);
        }
        boolean flag = smsNewCommodityService.saveBatch(list);
        return new Result(flag?Code.SAVE_OK: Code.SAVE_ERR,flag);
    }


    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsNewCommodityService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @GetMapping("/page")
    public IPage<SmsNewCommodity> getPage(@RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize,
                                          @RequestParam String productName) {
        IPage<SmsNewCommodity> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsNewCommodity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("product_name", productName);
        return smsNewCommodityService.page(page,queryWrapper);
    }

    @ApiOperation(value = "根据改变推荐状态")
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id) {
        SmsNewCommodity smsNewCommodity = smsNewCommodityService.getById(id);
        if(smsNewCommodity.getRecommendStatus()==1)
        {
            smsNewCommodity.setRecommendStatus(0);
        }else{
            smsNewCommodity.setRecommendStatus(1);;
        }
        boolean flag = smsNewCommodityService.save(smsNewCommodity);
        return new Result(flag?Code.UPDATE_OK: Code.UPDATE_ERR,flag);
    }
}
